package com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.impl;

import com.cicc.beergamevirtualsimulationsystem.common.webSocket.config.WebSocketMessageSender;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.JsonMessageHandler;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.Impl.GameEndJsonWebSocketMessage;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.WebSocketMessageTypeEnum;
import com.cicc.beergamevirtualsimulationsystem.entity.RoomInfoManager;
import com.cicc.beergamevirtualsimulationsystem.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Component
public class GameEndJsonMessageHandler implements JsonMessageHandler<GameEndJsonWebSocketMessage> {
    @Autowired
    private RoomService roomService;
    @Override
    public void handle(WebSocketSession session, GameEndJsonWebSocketMessage message) {
        String roomId = session.getAttributes().get("roomId").toString();

        roomService.closeRoom(roomId);
        RoomInfoManager.clearRoom(roomId,0);
        WebSocketMessageSender.sendMessageToRoom(roomId, new GameEndJsonWebSocketMessage("游戏结束"));
    }

    @Override
    public String type() {
        return WebSocketMessageTypeEnum.GAME_END.getValue();
    }

    @Override
    public Class<GameEndJsonWebSocketMessage> getMessageClass() {
        return GameEndJsonWebSocketMessage.class;
    }
}
